#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "gerenciadorArquivo.h"
#include "tipoAudio.h"

//le o arquivo e retorna o numero de canais
void ler_arquivo(char *nome_arquivo, DataBase *data){
	FILE *finput;
	wav_header cabecalho;
	int num_amostras;
	unsigned short int *amostras_entrada;

	if((finput = fopen(nome_arquivo, "r")) == NULL)
	{
		printf("Nao foi possivel abrir %s \n", nome_arquivo);
	}
	
	//Lendo o cabecalho
	fread(&cabecalho,sizeof(wav_header),1,finput);
    data->NoC = cabecalho.numChannels;
    data->dataSize = ftell(finput);
	fseek(finput, 0, SEEK_END);
	data->dataSize = 8*(ftell(finput) - data->dataSize);
	
	//Reposicionando ponteiro para leitura
	fseek(finput, sizeof(wav_header), SEEK_SET);
	num_amostras = data->dataSize/16;
	
	//Lendo as amostras
	data->data = (unsigned short int *) malloc(sizeof(unsigned short int) * num_amostras);
	fread(data->data, sizeof(unsigned short int), num_amostras, finput);
	
	//Fechando o Arquivo
	fclose(finput);
}

void decodificar_amostras(unsigned char *camostras, int noChar, unsigned short * samostras){
	int i, j;
	int tamShort = (1/2)*noChar;

	j=0;
	for(i=0; i<tamShort; i++){
		samostras[i] = camostras[j++]*256;
		samostras[i] = samostras[i]+ camostras[j++];
	}
}

void ler_arquivo_comprimido(DataBase *data, char *comp){
	FILE *arq;
	unsigned char *amostras;
	int num_char;

	arq = fopen(comp, "rb");

	fread(&(data->NoC),sizeof(unsigned short int), 1,arq);

	//Descobrindo o tamanho do arquivo
	num_char = ftell(arq);
	fseek(arq,0,SEEK_END);
	num_char = ftell(arq) - num_char;
	data->dataSize = num_char/2;

	//Reposicionando ponteiro para leitura
	fseek(arq, sizeof(unsigned short int), SEEK_SET);

	//Lendo as amostras
	amostras = (unsigned char *) malloc(sizeof(unsigned char)* num_char);
	fread(amostras,sizeof(unsigned char),num_char,arq);

	fclose(arq);

	/* Convertendo Amostras */
	data->data =(unsigned short int*)malloc(sizeof(unsigned short int)*((1/2)*num_char));
	decodificar_amostras(amostras, num_char, data->data);
}

void escrever_arquivo_comprimido(DataBase *data, char *nome_arquivo){
	FILE *saida;
	int i, j;

	saida = fopen(nome_arquivo, "w");
	fwrite(&(data->NoC), sizeof(unsigned short), 1, saida);
	for(i=0; i<data->dataSize; i++){
		fprintf(saida, "%u", data->data[i]);
	}
	if(data->choosenOne != NULL)
		fprintf(saida, "%c", *(data->choosenOne));
}

void escrever_arquivo (unsigned char *data, int dataSize, unsigned char *signal, int signalSize, int signalRest, char *fileName, short NoC){
	FILE *saida;
	wav_header cabecalho;
	inicializar_header(&cabecalho);
	cabecalho.numChannels = NoC;
	int i;
	if((saida = fopen(fileName, "wb")) == NULL)
		{
			printf("Nao foi possivel abrir %s \n", fileName);
		}

	fwrite(&cabecalho, sizeof(wav_header), 1, saida);
	fprintf(saida, "%c", (char)dataSize);
	fprintf(saida, "%c", (char)signalSize);
    fprintf(saida, "%c", (char)signalRest);
	
    for(i=0; i<dataSize; i++){
		fprintf(saida, "%c", data[i]);
	}
	
	for(i=0; i<signalSize; i++){
		fprintf(saida, "%c", signal[i]);
	}

	fclose(saida);
}
